package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.PmsCategoryDao;
import com.atguigu.gulimall.product.entity.PmsCategoryEntity;
import com.atguigu.gulimall.product.service.PmsCategoryService;


@Service("pmsCategoryService")
public class PmsCategoryServiceImpl extends ServiceImpl<PmsCategoryDao, PmsCategoryEntity> implements PmsCategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PmsCategoryEntity> page = this.page(
                new Query<PmsCategoryEntity>().getPage(params),
                new QueryWrapper<PmsCategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<PmsCategoryEntity> listWithCategory() {
        //查询出全部分类
        List<PmsCategoryEntity> pmsCategoryEntities = baseMapper.selectList(null);
        System.out.println(pmsCategoryEntities);
        //将收集起来的进行刷选
        List<PmsCategoryEntity> categoryEntities = pmsCategoryEntities.stream()
                .filter(pmsCategoryEntity ->

                pmsCategoryEntity.getParentCid() .equals(0)
        )
                .map((menu)->{//这里map映射，相当于循环了整个列表，menu相当于根节点，可能是一级菜单也可能是二级菜单
                    menu.setChirdren(getChirdren(menu,pmsCategoryEntities));
                    return menu;
                })
                .sorted((catagory1,category2)->{
                    return ((catagory1.getSort()==null)?0:catagory1.getSort())
                            -((category2.getSort()==null)?0:category2.getSort());
                })
                .collect(Collectors.toList());
        return categoryEntities;
    }

    @Override
    public int deleByIds(Long[] catIds) {
        //TODO 根据服务哪些被应用那些就不能被删除
        int i = baseMapper.deleteBatchIds(Arrays.asList(catIds));
        return i;
    }

    public List<PmsCategoryEntity> getChirdren(PmsCategoryEntity root,List<PmsCategoryEntity> all){
        List<PmsCategoryEntity> chirdrenList = all.stream()
                .filter(pmsCategoryEntity ->
            //数值-128~127,int.其他integer。会自动包装无法使用==进行比较，所以对于数据库要比较数值相等问题，建议equals
                        pmsCategoryEntity.getParentCid().equals(root.getCatId()))
                .map((pmsCategoryEntity)->{
                    pmsCategoryEntity.setChirdren(getChirdren(pmsCategoryEntity,all));
                    return pmsCategoryEntity;
                })
                        .sorted((catagory1,category2)->{
            return ((catagory1.getSort()==null)?0:catagory1.getSort())
                    -((category2.getSort()==null)?0:category2.getSort());
        })
                        .collect(Collectors.toList());
        return chirdrenList;
    }


}